/**
 * Java RTP Library (jlibrtp)
 * Copyright (C) 2006 Arne Kepp
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jlibrtp;

import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The purpose of this thread is to check whether there are packets ready from
 * any participants.
 *
 * It should sleep when not in use, and be woken up by a condition variable.
 *
 * Optionally, if we do jitter-control, the condition variable should have a max waiting period
 * equal to how often we need to push data.
 *
 * @author Arne Kepp
 */
public class AppCallerThread extends Thread {
    /** Logger instance. */
    private static final Logger LOGGER =
        Logger.getLogger(AppCallerThread.class.getName());

    /**  The parent RTP Session */
    RTPSession rtpSession;
    /**  The applications interface, where the callback methods are called */
    RTPAppIntf appl;

    /**
     * Instantiates the AppCallerThread
     *
     * @param session the RTPSession with participants etc
     * @param rtpApp the interface to which data is given
     */
    protected AppCallerThread(RTPSession session, RTPAppIntf rtpApp) {
        rtpSession = session;
        appl = rtpApp;
        if(LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("<-> AppCallerThread created");
        }
        setName("AppCallerThread");
    }

    /**
     * The AppCallerThread will run in this loop until the RTPSession
     * is terminated.
     *
     * Whenever an RTP packet is received it will loop over the
     * participants to check for packet buffers that have available
     * frame.
     */
    public void run() {
        if(LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer("-> AppCallerThread.run()");
        }

        while(rtpSession.endSession == false) {

            rtpSession.pktBufLock.lock();
            try {
                if(LOGGER.isLoggable(Level.FINER)) {
                    LOGGER.finer("<-> AppCallerThread going to Sleep");
                }

                try { rtpSession.pktBufDataReady.await(); }
                catch (Exception e) { LOGGER.log(Level.WARNING, "AppCallerThread:" + e.getMessage(), e);}

                // Next loop over all participants and check whether they have anything for us.
                Enumeration<Participant> enu = rtpSession.partDb.getParticipants();

                while(enu.hasMoreElements()) {
                    Participant p = enu.nextElement();

                    boolean done = false;
                    //System.out.println(p.ssrc + " " + !done +" " + p.rtpAddress
                    //		+ " " + rtpSession.naiveReception + " " + p.pktBuffer);
                    //System.out.println("done: " + done + "  p.unexpected: " + p.unexpected);
                    while(!done && (!p.unexpected || rtpSession.naiveReception)
                            && p.pktBuffer != null && p.pktBuffer.length > 0) {

                        DataFrame aFrame = p.pktBuffer.popOldestFrame();
                        if(aFrame == null) {
                            done = true;
                        } else {
                            appl.receiveData(aFrame, p);
                        }
                    }
                }

            } finally {
                rtpSession.pktBufLock.unlock();
            }

        }
        if(LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer("<- AppCallerThread.run() terminating");
        }
    }

}
